from django.db import models

from django.core import validators

#This import is for thumbnails. :) :)
from django.contrib.thumbnails import *

class Publisher(models.Model):
    name = models.CharField(max_length=128, core=True, unique=True)
    slug = models.SlugField(max_length=128, prepopulate_from=("name", ), help_text="This field will automatically populate. Ignore it.", unique=True)
    link = models.URLField(max_length=256, blank=True)
    email = models.EmailField(blank=True)

    def __unicode__(self):
        return u'%s' % self.name

    class Admin:
        fields = (
            (None, {
                'fields': ('name', 'slug', 'link', 'email'),
            }),
        )
        list_display = ('name', )
		
class Publication(models.Model):
    title = models.CharField(max_length=256, core=True)
    slug = models.SlugField(max_length=128, prepopulate_from=("title", ), help_text="This field will automatically populate. Ignore it.", unique=True)
    image_file = models.ImageField(upload_to="album_art")
    media_format = models.CharField(max_length=128, blank=True)
    commentary = models.TextField(max_length=32768, blank=True, help_text="HTML is allowed (probably JavaScript too)")
    #short_credits = models.TextField(max_length=512, null=True, blank=True, help_text="HTML is allowed (probably JavaScript too)")
    long_credits = models.TextField(max_length=4096, blank=True, help_text="HTML is allowed (probably JavaScript too)")
    #is_for_sale = models.NullBooleanField(null=True, blank=True)
    published_by = models.ForeignKey(Publisher, null=True, blank=True)
    catalog_number = models.CharField(max_length=42, blank=True)
    year = models.CharField(max_length=4, blank=True, validator_list=[validators.isOnlyDigits], help_text="Enter a four-digit year, like 1982 or leave blank")
	
	
    def __unicode__(self):
        return u'%s' % self.title

    def _admin_thumb(self):
        """Returns an <img> tag pointing to a generated thumbnail file, suitable for the admin interface list view."""
        image_file_handle = open(self.get_image_file_filename())
        this_thumb = Thumbnail("admin_thumbs/"+self.image_file, size=(96,96), data=image_file_handle.read())
        image_file_handle.close()
        return '<img height="%s" width="%s" alt="%s" src="%s" />' % (this_thumb.thumbnail.size[1], this_thumb.thumbnail.size[0], self.title, this_thumb.url)
    _admin_thumb.allow_tags = True
    _admin_thumb.short_description = "Thumbnail"
	
    class Meta:
        ordering = ['-year', '-catalog_number']

    class Admin:
        fields = (
            (None, {
                'fields': ('title', 'slug', 'year', 'published_by', 'catalog_number', 'image_file', 'media_format', 'commentary', 'long_credits', ),
            }),
        )
        list_display = ('_admin_thumb', 'title', 'year', 'catalog_number', 'media_format', 'published_by', )
        list_filter = ('year', 'media_format', 'published_by', )
        js = (
            'http://static.bryanlewissaunders.org/js/tiny_mce/tiny_mce.js',
            'http://static.bryanlewissaunders.org/js/textareas.js',
        )
		
		
class Track(models.Model):
    number = models.SmallIntegerField(blank=False)
    name = models.CharField(max_length=256, core=True)
    length = models.CharField(max_length=6, blank=True, core=False)
	
    media_file = models.FileField(upload_to="tracks", blank=True)

    album = models.ForeignKey(Publication, edit_inline=models.TABULAR, num_in_admin=14)
	
    def __unicode__(self):
        return u'%s - %s' % (self.album, self.name)

    class Meta:
        ordering = ['album', 'number']
		
    class Admin:
        fields = (
            (None, {
                'fields': ('album', 'number', 'name', 'length', 'media_file', ),
            }),
        )
        list_display = ('album', 'number', 'name', 'length')
        list_filter = ('album', )